#include <QtGlobal>

#include <QtCore>
#include <QGuiApplication>
#include <QSslSocket>
#include <QTranslator>

#include "../include/Constance.h"

#include "Qml/Engine.h"
#include "Qml/TypeRegister.h"
#include "Utility/UiLoader.h"
#include "MyQmlNetworkAccessManagerFactory.h"

#ifdef __ANDROID__
#include "Platform/Android/AndroidEnv.h"
#endif

using namespace TC;
using namespace TC::Utility;

void loadTranslateFile(QGuiApplication &app, const QString &path)
{
	QTranslator *translator = new QTranslator;
	if (translator->load(path, ":/"))
	{
		qDebug() << "Translator loaded : " << path << endl;
		if (app.installTranslator(translator))
		{
			return;
		}
	}
	qWarning() << "Failed to load translator : " << path << endl;
}

int main(int argc, char *argv[])
{
	QCoreApplication::setOrganizationName("TCommunity");
	QCoreApplication::setOrganizationDomain("weizehua.vicp.net");
	QCoreApplication::setApplicationName("TCommunityClient");
	QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
	QGuiApplication app(argc, argv);

	// SSL check
	qDebug() << "SSL support: " << QSslSocket::supportsSsl() << endl;

	// translator
	loadTranslateFile(app, "tc_zh");

	// remote new UI
	UiLoader::instance()->tryLoadNewResources();

	// cpp types -> qml types
	Qml::registerAll();

	// android environment
#ifdef __ANDROID__
	Android::AndroidEnv::registerAll();
#endif

	// qml engine
	auto engine = Qml::Engine::instance();
	engine->setNetworkAccessManagerFactory(new MyQmlNetworkAccessManagerFactory);

	// load qt quick
#ifdef _TCDEBUG
	Qml::Engine::instance()->useWindowsFileSystem = true;
	engine->load(QUrl(QStringLiteral("./Qml/main.qml")));
#else
    engine->load(QUrl(QStringLiteral("qrc:/Qml/main.qml")));
#endif

	// exec
	return app.exec();
}
